﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SunLogic : MonoBehaviour {

    public int m_fWeatherCounter;
    public float m_fRainCounter;
    public int m_iRainIndex;

    int m_WeatherChgIdx;

    bool m_bIntensiveChged = false;
    bool m_bSunRising = false;

    bool m_bSunToRed;

    WEATHER_TYPE m_CurrentWeather;
    WEATHER_TYPE m_PrevWeather;
    public List<WEATHER_TYPE> m_WeatherList;

	// Use this for initialization
	void Start () {
        m_bSunToRed = true;

        m_WeatherChgIdx = (int)WEATHER_TYPE.END;

        RandWeatherCounter();
        m_fRainCounter = 75.0f;
        m_iRainIndex = 7;
        
        //m_WeatherList.Add(WEATHER_TYPE.SUNNY);
        m_WeatherList.Add(WEATHER_TYPE.RAINY);
        m_WeatherList.Add(WEATHER_TYPE.RAINY);
        m_WeatherList.Add(WEATHER_TYPE.RAINY);
        //for (int i = m_WeatherList.Count; i < 3; ++i)
        //{
        //    m_WeatherList.Add(RandNextWeather());
        //}
	}
	
	// Update is called once per frame
	void Update () {
        // 태양 밝기 조절
        SunIntensiveChg();
        // 태양 움직임
        SunMove();
        // 날씨 조절
        Weathering();
        WeatherChger();
	}

    public void SunSetRising(bool bRising)
    {
        m_bIntensiveChged = true;
        m_bSunRising = bRising;
        if (bRising)
        {
            transform.FindChild("SUN").FindChild("Flash").GetComponent<Light>().color = Color.white;
            m_bSunToRed = true;
        }
    }

    public void SunPosReset()
    {
        transform.FindChild("SUN").transform.localPosition = new Vector3(5, 5, 0);
    }

    void RandWeatherCounter()
    {
        m_fWeatherCounter = 25;
    }

    void WeatherChger()
    {
        if (m_CurrentWeather != m_PrevWeather)
        {
            switch (m_WeatherChgIdx)
            {
                case (int)WEATHER_TYPE.END:
                    break;

                case (int)WEATHER_TYPE.SUNNY:
                    if (StopRain())
                    {
                        if (ToSunny())
                            m_WeatherChgIdx = (int)WEATHER_TYPE.END;
                    }
                    break;

                case (int)WEATHER_TYPE.CLOUDY:
                    if (StopRain())
                    {
                        if (ToCloudy())
                            m_WeatherChgIdx = (int)WEATHER_TYPE.END;
                    }
                    break;

                case (int)WEATHER_TYPE.RAINY:
                    if (ToRainy())
                    {
                        m_WeatherChgIdx = (int)WEATHER_TYPE.END;
                        m_iRainIndex = 0;
                    }
                    break;
            }
        }
    }

    void Weathering()
    {
        if (WeatherCountChk())
        {
            m_PrevWeather = m_CurrentWeather;
            m_CurrentWeather = m_WeatherList[0];

            switch (m_WeatherList[0])
            {
                case WEATHER_TYPE.SUNNY:
                    m_WeatherChgIdx = (int)WEATHER_TYPE.SUNNY;
                    break;


                case WEATHER_TYPE.CLOUDY:
                    m_WeatherChgIdx = (int)WEATHER_TYPE.CLOUDY;
                    break;


                case WEATHER_TYPE.RAINY:
                    m_iRainIndex = 0;
                    m_WeatherChgIdx = (int)WEATHER_TYPE.RAINY;
                    if (m_CurrentWeather != m_PrevWeather)
                        GameObject.Find("Weather").GetComponent<AudioSource>().Play();
                    break;
            }

            m_WeatherList.RemoveAt(0);

            for (int i = m_WeatherList.Count; i < 3; ++i)
            {
                m_WeatherList.Add(RandNextWeather());
            }
        }
    }

    bool ToRainy()
    {
        if (ToCloudy())
        {
            if (m_iRainIndex > 6)
            {
                return true;
            }

            m_fRainCounter -= Clock.fElapseSpeed;

            if (m_fRainCounter < 0)
            {
                GameObject.Find("Weather").transform.GetChild(m_iRainIndex).gameObject.SetActive(true);
                m_iRainIndex += 1;
                m_fRainCounter = 75.0f;
                GameObject.Find("Weather").GetComponent<AudioSource>().volume += 0.07f;
            }
        }

        return false;
    }

    bool StopRain()
    {
        if(m_iRainIndex > 6)
        {
            GameObject.Find("Weather").GetComponent<AudioSource>().volume = 0.0f;
            GameObject.Find("Weather").GetComponent<AudioSource>().Stop();
            return true;
        }

        m_fRainCounter -= Clock.fElapseSpeed;

        if (m_fRainCounter < 0)
        {
            GameObject.Find("Weather").transform.GetChild(m_iRainIndex).gameObject.SetActive(false);
            m_iRainIndex += 1;
            m_fRainCounter = 75.0f;
            GameObject.Find("Weather").GetComponent<AudioSource>().volume -= 0.07f;
        }

        return false;
    }

    bool ToCloudy()
    {
        if (transform.FindChild("SUN").FindChild("Flash").localPosition.z < 0)
            transform.FindChild("SUN").FindChild("Flash").localPosition = new Vector3(0, 0, transform.FindChild("SUN").FindChild("Flash").localPosition.z + (Time.deltaTime * 0.5f));
        else
        {
            transform.FindChild("SUN").FindChild("Flash").localPosition = Vector3.zero;
            return true;
        }

        return false;
    }

    bool ToSunny()
    {
        if (transform.FindChild("SUN").FindChild("Flash").localPosition.z > -1.5f)
            transform.FindChild("SUN").FindChild("Flash").localPosition = new Vector3(0, 0, transform.FindChild("SUN").FindChild("Flash").localPosition.z - (Time.deltaTime * 0.5f));
        else
        {
            transform.FindChild("SUN").FindChild("Flash").localPosition = new Vector3(0, 0, -1.5f);
            return true;
        }

        return false;
    }

    bool WeatherCountChk()
    {
        if (m_fWeatherCounter <= 0)
        {
            RandWeatherCounter();

            return true;
        }

        return false;
    }

    WEATHER_TYPE RandNextWeather()
    {
        WEATHER_TYPE tmpWeather;

        int iTempRand = Random.Range(0, 100);
        if (iTempRand < 40)
            tmpWeather = WEATHER_TYPE.RAINY;
        else if (iTempRand < 70)
            tmpWeather = WEATHER_TYPE.CLOUDY;
        else
            tmpWeather = WEATHER_TYPE.SUNNY;

        return tmpWeather;
    }

    public void WeatherCounting()
    {
        m_fWeatherCounter -= 1;
    }

    void SunMove()
    {
        transform.FindChild("SUN").transform.localPosition = new Vector3(transform.FindChild("SUN").transform.localPosition.x - (0.015f * Clock.fElapseSpeed), 5, 0);
    }

    void SunIntensiveChg()
    {
        if (m_bIntensiveChged)
        {
            if (m_bSunRising)
            {
                transform.FindChild("SUN").GetComponent<Light>().intensity += 0.0025f * Clock.fElapseSpeed;
                if (transform.FindChild("SUN").GetComponent<Light>().intensity > 0.3f)
                {
                    transform.FindChild("SUN").GetComponent<Light>().intensity = 0.3f;
                    m_bIntensiveChged = false;
                }
            }
            else
            {
                if (m_bSunToRed)
                {
                    if (transform.FindChild("SUN").FindChild("Flash").GetComponent<Light>().color.b > 0)
                        transform.FindChild("SUN").FindChild("Flash").GetComponent<Light>().color = new Color(1,
                                                                                                        transform.FindChild("SUN").FindChild("Flash").GetComponent<Light>().color.g - ((Clock.fElapseSpeed * 3.5f / 2f) / 255f),
                                                                                                        transform.FindChild("SUN").FindChild("Flash").GetComponent<Light>().color.b - (Clock.fElapseSpeed * 3.5f / 255f));
                    else
                        m_bSunToRed = false;
                }
                else
                    transform.FindChild("SUN").FindChild("Flash").GetComponent<Light>().color = new Color(transform.FindChild("SUN").FindChild("Flash").GetComponent<Light>().color.r - (Clock.fElapseSpeed * 3.5f / 255f),
                                                                                                        transform.FindChild("SUN").FindChild("Flash").GetComponent<Light>().color.g - ((Clock.fElapseSpeed * 3.5f / 2f) / 255f),
                                                                                                        0);

                transform.FindChild("SUN").GetComponent<Light>().intensity -= 0.0025f * Clock.fElapseSpeed;
                
                if (transform.FindChild("SUN").GetComponent<Light>().intensity < 0)
                {
                    transform.FindChild("SUN").GetComponent<Light>().intensity = 0;
                    m_bIntensiveChged = false;
                }
            }
        }
    }
}
